package com.framework.mapper.generator.godeGenerator.custom;

import com.framework.common.util.hump.HumpOrLineUtil;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.mapper.generator.godeGenerator.custom
 * @description 自定义mapper泛型扩展类
 * @datetime 2025-05-09 11:09:23
 */
public class CustomJavaMapperGenericMapperPlugin extends PluginAdapter {
    // model 前缀
    private String modelVoPreffix;

    // model 后缀
    private String modelVoSuffix;

    // 父类路径
    private String mapperClassPath;

    private boolean selectByPrimaryKeyIs = true;

    @Override
    public boolean validate(List<String> warnings) {
        mapperClassPath = properties.getProperty("rootInterface");
        modelVoPreffix = properties.getProperty("modelVoPreffix");
        modelVoPreffix = stringHasValue(modelVoPreffix) ? modelVoPreffix : "";
        modelVoSuffix = properties.getProperty("modelVoSuffix");
        modelVoSuffix = stringHasValue(modelVoSuffix) ? modelVoSuffix : "";
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        String recordType = introspectedTable.getBaseRecordType();
        String mapperClassName = mapperClassPath.substring(mapperClassPath.lastIndexOf(".") + 1);
        String modelName = recordType.substring(recordType.lastIndexOf(".") + 1);
        String modelVoName = HumpOrLineUtil.stringToFirstOneUpperCase(modelVoPreffix.toLowerCase()) + modelName + HumpOrLineUtil.stringToFirstOneUpperCase(modelVoSuffix.toLowerCase());
        String classVoPath = recordType.substring(0, recordType.lastIndexOf(".") + 1) + modelVoSuffix.toLowerCase() + "." + modelVoName;
        // 添加继承语句
        interfaze.addSuperInterface(
                new FullyQualifiedJavaType(mapperClassName + "<" + modelVoName + ">")
        );
        // 添加泛型所需的import语句
        interfaze.addImportedType(new FullyQualifiedJavaType(mapperClassPath));
        interfaze.addImportedType(new FullyQualifiedJavaType(classVoPath));
        selectByPrimaryKeyIs = true;
        return true;
    }

    @Override
    public boolean sqlMapBaseColumnListElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        //禁止生成mybatis自带 Base_Column_List 的 mapper xml 内容
        return false;
    }

    @Override
    public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        //禁止生成mybatis自带 selectByPrimaryKey 方法的 mapper xml 内容
        return false;
    }

    @Override
    public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        //禁止生成mybatis自带 selectByPrimaryKey 方法的 mapper java 内容
        if (method.getName().equalsIgnoreCase("selectByPrimaryKey") && selectByPrimaryKeyIs) {
            selectByPrimaryKeyIs = false;
            return false;
        }
        return true;
    }
}
